﻿package mortal.game.view.pack
{
    import Message.Game.*;
    import Message.Public.*;
    import com.mui.controls.*;
    import com.mui.events.*;
    import com.mui.manager.*;
    import com.mui.utils.*;
    import extend.language.*;
    import fl.controls.*;
    import fl.data.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.cache.packCache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.cd.*;
    import mortal.game.view.common.guide.navbar.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.pack.data.*;
    import mortal.game.view.palyer.view.*;
    import mortal.game.view.wizard.*;
    import mortal.mvc.core.*;

    public class PackModule extends BaseWindow
    {
        private var _packTabData:Array;
        private var _packPageData:Array;
        private var _pageSize:int = 49;
        private var _posType:int = 0;
        private var _tabBar:GTabarNew;
        private var _packItemPanel:GTileList;
        private var _bottomPart:PackBottomPart;
        private var _dragable:Boolean = true;
        private var _dropable:Boolean = true;

        public function PackModule()
        {
            this._packTabData = Language.getArray(30057);
            this._packPageData = [{name:"page1", label:"1", pageIndex:0}, {name:"page2", label:"2", pageIndex:1}, {name:"page3", label:"3", pageIndex:2}, {name:"page4", label:"4", pageIndex:3}];
            this.init();
            return;
        }// end function

        private function init() : void
        {
            setSize(345, 500);
            title = Language.getString(20021);
            return;
        }// end function

        override protected function setWindowCenter() : void
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.pushUIToDisposeVec(UIFactory.bg(8, 62, 333, 328, this, ImagesConst.PanelBg));
            this._tabBar = UIFactory.gTabBarNew(2, 32, this._packTabData, 343, 500, 65, 26, this, this.tabBarChangeHandler, "TabButtonNew");
            this._tabBar.selectedIndex = 0;
            this._packItemPanel = UIFactory.tileList(10, 65, 344, 322, this);
            this._packItemPanel.columnWidth = 46;
            this._packItemPanel.rowHeight = 46;
            this._packItemPanel.horizontalGap = -1;
            this._packItemPanel.verticalGap = 0;
            this._packItemPanel.setStyle("cellRenderer", PackCellRenderer);
            this._packItemPanel.configEventListener(DragEvent.Event_Move_In, this.moveInBagHandler);
            this._packItemPanel.isShowVerticalScrollBar = true;
            this._packItemPanel.direction = ScrollBarDirection.VERTICAL;
            this._packItemPanel.verticalScrollPolicy = ScrollPolicy.ON;
            this._bottomPart = UICompomentPool.getUICompoment(PackBottomPart);
            this._bottomPart.createDisposedChildren();
            this._bottomPart.x = 9;
            this._bottomPart.y = 360;
            this.addChild(this._bottomPart);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._tabBar.dispose(param1);
            this._bottomPart.dispose(param1);
            this._packItemPanel.dispose(param1);
            this._tabBar = null;
            this._bottomPart = null;
            this._packItemPanel = null;
            Cache.instance.cd.unregisterCDData(CDDataType.backPackLock, "lockCD");
            return;
        }// end function

        private function moveInBagHandler(event:DragEvent) : void
        {
            var _loc_2:BaseItem = null;
            var _loc_3:BaseItem = null;
            var _loc_4:ItemData = null;
            var _loc_5:ItemData = null;
            var _loc_6:DragDropData = null;
            if (this._dropable)
            {
                _loc_2 = event.dragItem as BaseItem;
                _loc_3 = event.dropItem as BaseItem;
                _loc_4 = event.dragSouce as ItemData;
                _loc_5 = _loc_3.itemData;
                if (_loc_2 && _loc_3 && _loc_2 == _loc_3 || _loc_2 == null || this._tabBar.selectedIndex != 0)
                {
                    return;
                }
                if (_loc_4 && (this._posType == EPlayerItemPosType._EPlayerItemPosTypeBag || this._posType == _loc_4.posType))
                {
                    _loc_6 = new DragDropData(_loc_4.serverData.posType, this._posType, _loc_4.serverData.uid, _loc_2.pos, _loc_3.pos, _loc_4, _loc_5);
                    Dispatcher.dispatchEvent(new DataEvent(EventName.BackPack_DragInItem, _loc_6));
                }
            }
            event.stopImmediatePropagation();
            return;
        }// end function

        private function checkPageTabBarDrag(param1:IDragDrop) : Boolean
        {
            return false;
        }// end function

        private function tabBarChangeHandler(event:MuiEvent = null) : void
        {
            this.updateAllItems();
            return;
        }// end function

        private function pageBarChangeHandler(event:MuiEvent) : void
        {
            this.updateAllItems();
            this._packItemPanel.selectedIndex = -1;
            return;
        }// end function

        private function getDataProvider() : DataProvider
        {
            var _loc_2:Array = null;
            var _loc_3:int = 0;
            var _loc_4:BackPackCache = null;
            var _loc_5:Object = null;
            var _loc_7:int = 0;
            var _loc_1:* = new DataProvider();
            _loc_4 = Cache.instance.pack.backPackCache;
            switch(this._tabBar.selectedIndex)
            {
                case 0:
                {
                    _loc_7 = Math.ceil(_loc_4.currentGrid / 7) * 7;
                    if (_loc_7 < 49)
                    {
                        this._pageSize = 49;
                    }
                    else if (_loc_7 > _loc_4.totalGride)
                    {
                        this._pageSize = _loc_4.totalGride;
                    }
                    else
                    {
                        this._pageSize = _loc_7;
                    }
                    _loc_2 = _loc_4.getItemsAtPage(1, this._pageSize);
                    break;
                }
                case 1:
                {
                    this._pageSize = _loc_4.currentGrid;
                    _loc_2 = _loc_4.getEquipItemsAtPage();
                    break;
                }
                case 2:
                {
                    this._pageSize = _loc_4.currentGrid;
                    _loc_2 = _loc_4.getPropItemsAtPage();
                    break;
                }
                case 3:
                {
                    this._pageSize = _loc_4.currentGrid;
                    _loc_2 = _loc_4.getStuffsItemsAtPage();
                    break;
                }
                default:
                {
                    break;
                }
            }
            _loc_3 = _loc_2.length;
            var _loc_6:int = 0;
            while (_loc_6 < _loc_3)
            {
                
                _loc_5 = this.getDataObjectByItemData(_loc_2[_loc_6]);
                _loc_1.addItem(_loc_5);
                _loc_6++;
            }
            return _loc_1;
        }// end function

        private function getDataObjectByItemData(param1:ItemData) : PackObject
        {
            var _loc_3:Dictionary = null;
            var _loc_2:* = new PackObject();
            if (param1)
            {
                _loc_2.itemData = param1;
                if (param1.itemInfo)
                {
                    if (GameController.trade.isViewShow)
                    {
                        if (ItemsUtil.isBind(param1))
                        {
                            _loc_2.locked = true;
                        }
                        _loc_3 = Cache.instance.trade.usedItems;
                        if (_loc_3[param1.uid])
                        {
                            _loc_2.used = true;
                        }
                    }
                    else if (Cache.instance.guild.selfGuildInfo.wareHouse.isOpen)
                    {
                        if (ItemsUtil.isBind(param1))
                        {
                            _loc_2.locked = true;
                        }
                    }
                }
            }
            return _loc_2;
        }// end function

        private function rollToCurrentIndex() : void
        {
            var _loc_2:int = 0;
            var _loc_1:* = Cache.instance.pack.backPackCache.currentGrid - Cache.instance.pack.backPackCache.capacity - 1;
            if (_loc_1 > 0)
            {
                _loc_2 = Cache.instance.pack.backPackCache.capacity + 1;
                this._packItemPanel.scrollToIndex(_loc_2);
            }
            return;
        }// end function

        public function updateAllItems() : void
        {
            this._packItemPanel.dataProvider = this.getDataProvider();
            this.rollToCurrentIndex();
            return;
        }// end function

        public function updateItems() : void
        {
            if (!_disposed)
            {
                this._packItemPanel.dataProvider = this.getDataProvider();
            }
            return;
        }// end function

        public function setCoinAmount(param1:int) : void
        {
            if (this._bottomPart)
            {
                this._bottomPart.setCoinAmount(param1);
            }
            return;
        }// end function

        public function setCoinBindAmount(param1:int) : void
        {
            if (this._bottomPart)
            {
                this._bottomPart.setCoinBindAmount(param1);
            }
            return;
        }// end function

        public function setGoldAmount(param1:int) : void
        {
            if (this._bottomPart)
            {
                this._bottomPart.setGoldAmount(param1);
            }
            return;
        }// end function

        public function setGoldBindAmount(param1:int) : void
        {
            if (this._bottomPart)
            {
                this._bottomPart.setGoldBindAmount(param1);
            }
            return;
        }// end function

        public function updateCapacity() : void
        {
            if (this._bottomPart)
            {
                this._bottomPart.setCapacity();
            }
            return;
        }// end function

        public function updateMoney() : void
        {
            var _loc_1:* = Cache.instance.role.money;
            this.setCoinAmount(_loc_1.coin);
            this.setCoinBindAmount(_loc_1.coinBind);
            this.setGoldAmount(_loc_1.gold);
            this.setGoldBindAmount(_loc_1.goldBind);
            return;
        }// end function

        public function updataPackItemPanelSelectItem(param1:int) : void
        {
            if (this._packItemPanel)
            {
                this._packItemPanel.selectedIndex = param1;
            }
            return;
        }// end function

        public function showUnLockItem(param1:int) : void
        {
            var _loc_2:* = Cache.instance.pack.backPackCache;
            var _loc_3:* = param1;
            var _loc_4:* = new Array();
            var _loc_5:* = _loc_2.currentGrid - 1;
            while (_loc_5 < _loc_3)
            {
                
                _loc_4.push(_loc_5);
                _loc_5++;
            }
            this._packItemPanel.selectedIndices = _loc_4;
            Dispatcher.dispatchEvent(new DataEvent(EventName.PackSelectIndex, _loc_3));
            return;
        }// end function

        public function hideUnLockItem() : void
        {
            if (this._packItemPanel)
            {
                this._packItemPanel.selectedIndices = null;
            }
            return;
        }// end function

        public function changeTabIndex(param1:int) : void
        {
            this._tabBar.selectedIndex = param1;
            this.tabBarChangeHandler();
            return;
        }// end function

        public function getPointByCode(param1:int) : Point
        {
            var _loc_2:Array = null;
            var _loc_3:int = 0;
            switch(this._tabBar.selectedIndex)
            {
                case 0:
                {
                    _loc_2 = Cache.instance.pack.backPackCache.allItems;
                    break;
                }
                case 2:
                {
                    _loc_2 = Cache.instance.pack.backPackCache.propItems;
                    break;
                }
                case 3:
                {
                    _loc_2 = Cache.instance.pack.backPackCache.stuffsItems;
                    break;
                }
                case 1:
                {
                    _loc_2 = Cache.instance.pack.backPackCache.taskItems;
                    break;
                }
                default:
                {
                    break;
                }
            }
            var _loc_4:int = 0;
            while (_loc_4 < _loc_2.length)
            {
                
                if (_loc_2[_loc_4] && param1 == (_loc_2[_loc_4] as ItemData).itemCode)
                {
                    _loc_3 = _loc_4;
                    break;
                }
                _loc_4++;
            }
            var _loc_5:* = int(_loc_3 / 7);
            this._packItemPanel.scrollToIndex(_loc_3);
            return new Point(this._packItemPanel.x + 3 + _loc_3 % 7 * 45, this._packItemPanel.y + _loc_5 * 45 - this._packItemPanel.verticalScrollPosition + 3);
        }// end function

        public function getCanOpenGridPos() : Rectangle
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:Rectangle = null;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:Array = null;
            var _loc_8:int = 0;
            var _loc_1:* = Cache.instance.pack.backPackCache.currentGrid - Cache.instance.pack.backPackCache.capacity - 1;
            if (_loc_1 > 0)
            {
                _loc_2 = Cache.instance.pack.backPackCache.capacity + 1;
                _loc_3 = int(_loc_2 / 7);
                this._packItemPanel.scrollToIndex(_loc_2);
                _loc_6 = Cache.instance.pack.backPackCache.currentGrid / 7;
                if (_loc_3 != _loc_6)
                {
                    _loc_5 = 7 - (_loc_2 - 1) % 7;
                }
                else
                {
                    _loc_5 = _loc_1;
                }
                _loc_7 = new Array();
                _loc_8 = _loc_2 - 1;
                while (_loc_8 < (Cache.instance.pack.backPackCache.currentGrid - 1))
                {
                    
                    _loc_7.push(_loc_8);
                    _loc_8++;
                }
                this._packItemPanel.selectedIndices = _loc_7;
                _loc_4 = new Rectangle(this._packItemPanel.x + 2 + (_loc_2 % 7 - 1) * 45, this._packItemPanel.y + _loc_3 * 45 - this._packItemPanel.verticalScrollPosition + 7, _loc_5 * 45, 45);
                return _loc_4;
            }
            else
            {
                return null;
            }
        }// end function

        override public function hide() : void
        {
            super.hide();
            if (GameController.warehouse.isViewShow)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.WarehouseClose));
            }
            else if (GameController.shop.isViewShow)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShopClose));
            }
            var _loc_1:* = Cache.instance.cd.getCDDataByKeyType(CDDataType.backPackLock, "lockCD") as CDData;
            if (_loc_1)
            {
                _loc_1.stopCoolDown();
            }
            if (Cache.instance.pack.backPackCache.canOpenGridNum > 0)
            {
                NavbarButtonUIRow.instance.showHideFilter(ModuleType.Pack, true);
            }
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            NavbarButtonUIRow.instance.showHideFilter(ModuleType.Pack, false);
            super.show(param1, param2);
            if (FashionAdvanceWin.hasInstance() && !FashionAdvanceWin.instance.isHide)
            {
                return;
            }
            return;
        }// end function

        public function scrollToIndex(param1:int) : void
        {
            this._packItemPanel.scrollToIndex(param1);
            this._bottomPart.showCapacityFiler();
            return;
        }// end function

    }
}
